#include "State.h"

MainMenuState::MainMenuState(RenderWindow* window) : State(window, MAIN_MENU) {
    background = new Sprite();
    background->SetImage(imageManager.getImage(guiXml->Attribute("background")));
    
    TiXmlElement* titleXml = guiXml->FirstChildElement("title");    
    title = new Animated(titleXml->Attribute("name"));
    title->getSprite().SetPosition(Chameleon(titleXml->Attribute("x")),
                       Chameleon(titleXml->Attribute("y")));
    
    // Initiate list of levels
    // TODO: wtf... use an enum or something
    choiceNames.push_back("New Game");
    choiceNames.push_back("Options");
    choiceNames.push_back("Credits");
    choiceNames.push_back("Quit");
    
	// Create visible list of levels
    TiXmlElement* choiceXml    = getChildElementbyName("choice", guiXml);
    TiXmlElement* choicePosXml = choiceXml->FirstChildElement("position");
    for (int i = 0, y = Chameleon(choicePosXml->Attribute("y"));
         i < choiceNames.size();
         i++, y += Chameleon(choicePosXml->Attribute("vspace"))) {
        String choice(choiceNames.at(i));
        choice.SetColor(getColorFromXml(choiceXml->FirstChildElement("color")));
        choice.SetPosition(Chameleon(choicePosXml->Attribute("x")), y);
        choice.SetSize(Chameleon(choiceXml->Attribute("size")));
        
        choices.push_back(choice);
    }
    
    selected = 0;
    selectChoice(selected);
}

void MainMenuState::onEvent(Event event) {
    // Close window; exit
    if (event.Type == Event::Closed) {
        window->Close();
    }
    
    if (event.Type == Event::KeyPressed) {
        switch (event.Key.Code) {
            case Key::Escape:
                window->Close();
                break;
                
            case Key::Down:
                if (selected < choices.size() - 1) {
                    selectChoice(++selected);
                }
                break;
                
            case Key::Up:
                if (selected > 0) {
                    selectChoice(--selected);
                }
                break;
                
            case Key::Space:
                string chosen = getSelectedChoice();
                
                if (chosen == "New Game") {
                    nextState = LEVEL_SELECT;
                } else if (chosen == "Options") {
                    nextState = OPTIONS;
                } else if (chosen == "Credits") {
                    nextState = CREDITS;
                } else if (chosen == "Quit") {
                    window->Close();
                }
                break;
        }
    }
}

void MainMenuState::onHold() {
    // TODO: implement teh user-friendlies
}

void MainMenuState::listChoices() {
    for (int i = 0; i < choices.size(); i++) {
        window->Draw(choices.at(i));
    }
}

void MainMenuState::selectChoice(int choiceIndex) {
    TiXmlElement* choiceXml = getChildElementbyName("choice", guiXml);
    TiXmlElement* selectedChoiceXml = getChildElementbyName("selectedChoice", guiXml);
    
    for (int i = 0; i < choices.size(); i++) {
        if (i == choiceIndex) {
            choices.at(i).SetColor(getColorFromXml(selectedChoiceXml->FirstChildElement("color")));
        } else {
            choices.at(i).SetColor(getColorFromXml(choiceXml->FirstChildElement("color")));
        }
    }
}

void MainMenuState::render() {
    // Clear screen
    window->Clear();
    
    window->Draw(*background);
    window->Draw(title->getSprite());
    listChoices();
    
    // Display framerate
    if (SHOW_FRAMERATE) {
        framerate->SetText(getFramerate());
        window->Draw(*framerate);
    }
    
    // Finally, display the rendered frame on screen
    window->Display();
}

string MainMenuState::getSelectedChoice() {
    return choices.at(selected).GetText();
}

MainMenuState::~MainMenuState() {
    //codez
}
